var squareRoot = function(x){
  if(x <= 1){
    return x;
  }
  let left = 1;
  let right = x;
  let result = 1;
  while(left <= right){
    let mid = Math.floor(left + (right - left)/2);
    if(mid * mid <= x){
      result = mid;
      left = mid + 1;
    }else{
      right = mid - 1;
    }
    
  }
  return result;
}
console.log(squareRoot(16));